import urllib2
import upload
from xml.dom import minidom

class WebInt:
    def __init__(self,
    dev_key="9ABFHINUa8ea5eada546d9073057d1afda71d186"):
        self.dev_key=dev_key
        
    # Controlla se ImageShack e' online
    def pinger(self):
        req=urllib2.Request("http://www.imageshack.us/upload_api.php")
        try:
            urllib2.urlopen(req)
        except:
            return 0
        else:
            return 1
        
    def ImageShack(self, path, user, pwd):
        u=upload.Uploader(self.dev_key, username=user, password=pwd)
        try:
            risposta=u.uploadFile(path)
        except:
            return "error"
        else:
            return self.XmlParser(risposta)
        
    def XmlParser(self, risposta):
        xmlrisp=minidom.parseString(risposta)
        #print risposta
        links=xmlrisp.getElementsByTagName("image_link")
        link=links[0].toxml()
        link=link[12:-13]
        thumbs=xmlrisp.getElementsByTagName("thumb_link")
        thumb=thumbs[0].toxml()
        thumb=thumb[12:-13]
        
        return link, thumb
            